package com.briup.operator.service.impl;

import com.briup.common.exception.CustomerException;
import com.briup.common.util.PageResult;
import com.briup.common.web.MessageStatus;
import com.briup.common.web.SecurityUtil;
import com.briup.operator.entity.Operator;
import com.briup.operator.entity.extend.OperatorEx;
import com.briup.operator.mapper.OperatorMapper;
import com.briup.operator.mapper.extend.OperatorExtendMapper;
import com.briup.operator.service.IOpeatorService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 算子服务类实现类
 */
@Service
public class OperatorServiceImpl implements IOpeatorService {
    @Autowired
    private OperatorMapper operatorMapper;
    @Autowired
    private OperatorExtendMapper operatorExtendMapper;

    /*
     * @Description 新增算子信息
     * @Param [operator] 封装算子类
     * @return void
     **/
    @Transactional
    @Override
    public void insertOperator(Operator operator) {
        // 查出数据库已有的算子信息
        List<Operator> operatorList = operatorExtendMapper.findByNameOrUrl(operator.getOperatorName(), operator.getOperatorUrl());
        if (operatorList != null && operatorList.size() > 0) {
            throw new CustomerException(MessageStatus.DATA_EXISTED);
        }
        // 如果算子名称或者算子url存在,不能添加
        try {
            // 手动封装额外参数
            operator.setOperatorTime(new Date());
            operator.setUserId(Integer.parseInt(SecurityUtil.getUserId()));
            operator.setUserName(SecurityUtil.getUsername());
            operator.setOperatorType(1);
            operatorMapper.insert(operator);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomerException(e.getMessage());
        }
    }

    /*
     * @Description 查找算子详情
     * @Param [id] 算子id
     * @return 返回扩展了目录的算子类
     **/
    @Override
    public OperatorEx findById(Integer id) {
        OperatorEx operatorEx = operatorExtendMapper.findWithCategoryById(id);
        return operatorEx;
    }

    // 删除算子
    @Override
    public void deleteOperator(Integer id) {
        operatorMapper.deleteByPrimaryKey(id);
    }

    /*
     * @Description 条件并分页查询算子列表
     * @Param [operator 算子条件, pageNum 当前页, pageSize 每页条数]
     * @return 算子分页结果
     * 此处条件为算子类型和分类id
     **/
    @Override
    public PageResult<OperatorEx> findOperatorByCon(Operator operator, Integer pageNum, Integer pageSize) {
        // 分页
        PageHelper.startPage(pageNum, pageSize);
        Page<OperatorEx> operatorPage = operatorExtendMapper.findOperatorByCon(operator);

        return new PageResult<OperatorEx>(operatorPage.getTotal(), operatorPage.getResult());
    }

    @Override
    public Operator findByUserIdAndOperatorUrl(Integer userId, String operatorUrl) {
        return operatorExtendMapper.findByUserIdAndOperatorUrl(userId, operatorUrl);
    }

    @Override
    public List<OperatorEx> findOperatorByUserId(int userId) {
        return operatorExtendMapper.findOperatorExByUserId(userId);
    }
}
